/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-2-20
 * 
**/
package com.julewa.freemarker.method;

import org.springframework.beans.factory.annotation.Autowired;

import com.julewa.Client;
import com.julewa.Querier;

public class FM_URL implements Querier{
	@Autowired Client client;
	
	@Override
	public Object execute(Object... args){
		if(args.length == 0)return client.getUriRoot();
		StringBuffer sb = null;
		for(Object a:args){
			if(a == null)continue;
			if(sb == null){
				sb = getURL(a);
			}else{
				sb.append(a.toString());
			}
		}
		if(sb == null)return client.getUriRoot();
		return sb.toString();
	}
	
	private StringBuffer getURL(Object arg){
		StringBuffer url = new StringBuffer((String)arg);
		url.replace(0, (url.charAt(0) == '/')?1:0, client.getUriRoot());
		return url;
	}

}
